#!/bin/bash

# 数据管理系统curl测试脚本
# 本脚本演示如何使用curl命令测试数据管理系统的完整业务流程

# 服务器配置
BASE_URL="http://192.168.1.67:8081"
TOKEN=""
USER_ID=""
COMPANY_ID="1"

echo "=== 数据管理系统curl测试脚本 ==="

# 1. 用户登录
echo "1. 用户登录测试"
echo "发送POST请求到 /api/auth/login"
response=$(curl -s -X POST "${BASE_URL}/api/auth/login" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "123456"
  }')

echo "登录响应: $response"
echo ""

# 提取访问令牌
TOKEN=$(echo "$response" | grep -o '"token":"[^"]*' | grep -o '[^"]*$')
if [ -z "$TOKEN" ]; then
  echo "错误: 未能获取访问令牌"
  exit 1
fi
echo "访问令牌已获取: $TOKEN"
echo ""

# 2. 获取当前用户信息
echo "2. 获取当前用户信息"
echo "发送GET请求到 /api/auth/me"
response=$(curl -s -X GET "${BASE_URL}/api/auth/me" \
  -H "Authorization: Bearer $TOKEN")

echo "当前用户信息: $response"
echo ""

# 3. 创建新用户
echo "3. 创建新用户"
echo "发送POST请求到 /api/users"
response=$(curl -s -X POST "${BASE_URL}/api/users" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "testuser",
    "email": "test@example.com",
    "password": "testpass123",
    "fullName": "测试用户",
    "phoneNumber": "13800138005",
    "isActive": true
  }')

echo "创建用户响应: $response"
echo ""

# 提取新创建的用户ID
USER_ID=$(echo "$response" | grep -o '"id":[0-9]*' | grep -o '[0-9]*')
if [ -z "$USER_ID" ]; then
  echo "警告: 未能提取用户ID"
fi
echo "新用户ID: $USER_ID"
echo ""

# 4. 获取用户列表
echo "4. 获取用户列表"
echo "发送GET请求到 /api/users"
response=$(curl -s -X GET "${BASE_URL}/api/users" \
  -H "Authorization: Bearer $TOKEN")

echo "用户列表: $response"
echo ""

# 5. 创建数据集
echo "5. 创建数据集"
echo "发送POST请求到 /api/datasets"
response=$(curl -s -X POST "${BASE_URL}/api/datasets" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "测试数据集",
    "description": "这是一个测试数据集",
    "datasetType": "RAW",
    "filePath": "/path/to/file.csv",
    "fileSize": 1024,
    "fileFormat": "CSV",
    "rowCount": 100,
    "columnCount": 5
  }')

echo "创建数据集响应: $response"
echo ""

# 提取数据集ID
DATASET_ID=$(echo "$response" | grep -o '"id":[0-9]*' | grep -o '[0-9]*')
if [ -z "$DATASET_ID" ]; then
  echo "警告: 未能提取数据集ID"
fi
echo "数据集ID: $DATASET_ID"
echo ""

# 6. 获取数据集列表
echo "6. 获取数据集列表"
echo "发送GET请求到 /api/datasets"
response=$(curl -s -X GET "${BASE_URL}/api/datasets" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID")

echo "数据集列表: $response"
echo ""

# 7. 分析数据集
echo "7. 分析数据集"
echo "发送POST请求到 /api/datasets/$DATASET_ID/analyze"
response=$(curl -s -X POST "${BASE_URL}/api/datasets/${DATASET_ID}/analyze" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID")

echo "数据分析响应: $response"
echo ""

# 8. 创建处理任务
echo "8. 创建处理任务"
echo "发送POST请求到 /api/tasks"
response=$(curl -s -X POST "${BASE_URL}/api/tasks" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "数据清洗任务",
    "description": "清洗测试数据集",
    "sourceDatasetId": '$DATASET_ID',
    "processingType": "CLEANING"
  }')

echo "创建任务响应: $response"
echo ""

# 提取任务ID
TASK_ID=$(echo "$response" | grep -o '"id":[0-9]*' | grep -o '[0-9]*')
if [ -z "$TASK_ID" ]; then
  echo "警告: 未能提取任务ID"
fi
echo "任务ID: $TASK_ID"
echo ""

# 9. 启动任务
echo "9. 启动任务"
echo "发送POST请求到 /api/tasks/$TASK_ID/start"
response=$(curl -s -X POST "${BASE_URL}/api/tasks/${TASK_ID}/start" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID")

echo "启动任务响应: $response"
echo ""

# 10. 获取任务信息
echo "10. 获取任务信息"
echo "发送GET请求到 /api/tasks/$TASK_ID"
response=$(curl -s -X GET "${BASE_URL}/api/tasks/${TASK_ID}" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID")

echo "任务信息: $response"
echo ""

# 11. 停止任务
echo "11. 停止任务"
echo "发送POST请求到 /api/tasks/$TASK_ID/stop"
response=$(curl -s -X POST "${BASE_URL}/api/tasks/${TASK_ID}/stop" \
  -H "Authorization: Bearer $TOKEN" \
  -H "CompanyId: $COMPANY_ID" \
  -H "UserId: $USER_ID")

echo "停止任务响应: $response"
echo ""

# 12. 清理测试数据
echo "12. 清理测试数据"

# 删除任务
if [ ! -z "$TASK_ID" ]; then
  echo "删除任务 $TASK_ID"
  response=$(curl -s -X DELETE "${BASE_URL}/api/tasks/${TASK_ID}" \
    -H "Authorization: Bearer $TOKEN" \
    -H "CompanyId: $COMPANY_ID" \
    -H "UserId: $USER_ID")
  echo "删除任务响应: $response"
  echo ""
fi

# 删除数据集
if [ ! -z "$DATASET_ID" ]; then
  echo "删除数据集 $DATASET_ID"
  response=$(curl -s -X DELETE "${BASE_URL}/api/datasets/${DATASET_ID}" \
    -H "Authorization: Bearer $TOKEN" \
    -H "CompanyId: $COMPANY_ID" \
    -H "UserId: $USER_ID")
  echo "删除数据集响应: $response"
  echo ""
fi

# 更新用户状态为非活跃
if [ ! -z "$USER_ID" ]; then
  # 首先获取用户当前信息
  echo "获取用户 $USER_ID 当前信息"
  user_response=$(curl -s -X GET "${BASE_URL}/api/users/${USER_ID}" \
    -H "Authorization: Bearer $TOKEN")
  
  # 从用户信息中提取必要字段
  username=$(echo "$user_response" | sed -E 's/.*"username":"([^"]*)".*/\1/')
  email=$(echo "$user_response" | sed -E 's/.*"email":"([^"]*)".*/\1/')
  full_name=$(echo "$user_response" | sed -E 's/.*"fullName":"([^"]*)".*/\1/')
  phone_number=$(echo "$user_response" | sed -E 's/.*"phoneNumber":"([^"]*)".*/\1/')
  
  # 更新用户状态为非活跃
  echo "删除用户 $USER_ID"
  response=$(curl -s -X PUT "${BASE_URL}/api/users/${USER_ID}" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
    "username": "'"$username"'",
    "email": "'"$email"'",
    "fullName": "'"$full_name"'",
    "phoneNumber": "'"$phone_number"'",
    "isActive": false
    }'
)
  echo "删除用户响应: $response"
  echo ""
fi


echo "=== 测试完成 ==="